package com.hazardSoft.eStore.bussines;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.hazardSoft.eStore.common.exception.SystemException;
import com.hazardSoft.eStore.entities.Bill;
import com.hazardSoft.eStore.entities.Sale;
import com.hazardSoft.eStore.entities.User;
import com.hazardSoft.eStore.utils.PathConstants;
import com.hazardSoft.eStore.utils.QueryConstants;

/**
 * 
 * @author felipe
 *
 */
 public class UsersBO {

	private static final String CLASS_NAME = PathConstants.BUSSINES_PATH+UsersBO.class.getSimpleName();
	
	public User authenticateUser(User user, EntityManager entityManager) throws SystemException {
		User foundUser = this.searchUserByLogin(user, entityManager);
		if (!(foundUser == null )) {
			if (user.getPassword().equals(foundUser.getPassword())) {
				return foundUser;
			}
		}
		return null;
	}

	public User insertUser(User user, EntityManager entityManager) throws SystemException {
		if (this.searchUserByLogin(user, entityManager) == null) {
			if (user.getId() == null) {
				user.setId(0);
			}
			user.setLogin(user.getLogin().toLowerCase());
			entityManager.persist(user);
			
			return this.searchUserByLogin(user, entityManager);
		}
		return null;
	}
	
	public boolean removeUser(User user, EntityManager entityManager) throws SystemException {
		if (user == null) {
			throw new SystemException(new Exception("null user"), CLASS_NAME, "removeUser");
		}
		if (!(user.getLogin() == null)) {
			user = this.searchUserByLogin(user, entityManager);
			entityManager.remove(user);
			return true;
		} else {
			if (!(user.getId() == null)) {
				user = this.searchUserById(user, entityManager);
				entityManager.remove(user);
				return true;
			} else {
				throw new SystemException(new Exception("null user.id or user.login"), CLASS_NAME, "removeUser");
			}
		}
	}
	
	public User searchUserById(User user, EntityManager entityManager) throws SystemException {
		if (user == null) {
			throw new SystemException(new Exception("null user"), CLASS_NAME, "searchUserById");
		}
		
		if (!(user.getId() == null)) {
			return entityManager.find(User.class, user.getId());
		} else {
			throw new SystemException(new Exception("null user.id"), CLASS_NAME, "searchUserById");
		}
	}
	
	public User searchUserByLogin(User user, EntityManager entityManager) throws SystemException {
		if (user == null) {
			throw new SystemException(new Exception("null user"), CLASS_NAME, "searchUserByLogin");
		}
		if (user.getLogin() == null) {
			throw new SystemException(new Exception("null user.login"), CLASS_NAME, "searchUserByLogin");
		}
		Query query = entityManager.createNamedQuery(QueryConstants.QUERY_SEARCH_USER_BY_LOGIN);
		query.setParameter(1, user.getLogin().toLowerCase());
		
		List<User> users = query.getResultList();
		
		if (users.size() > 0) {
			return users.get(0);
		}
		return null;
	}
	
	public User updateUser(User user, EntityManager entityManager) throws SystemException {
		User tempUser = this.searchUserByLogin(user, entityManager); 
		if (!(tempUser == null)) {
			if (!(user.getAddress() == null)) {
				tempUser.setAddress(user.getAddress());
			}
			if(!(user.getCity() == null)) {
				tempUser.setCity(user.getCity());
			}
			if (!(user.getEmail() == null)) {
				tempUser.setEmail(user.getEmail());
			}
			if (!(user.getName() == null)) {
				tempUser.setName(user.getName());
			}
			if(!(user.getPassword() == null)) {
				tempUser.setPassword(user.getPassword());
			}
			if (!(user.getRole() == null)) {
				tempUser.setRole(user.getRole());
			}
			if (!(user.getSocialnumber() == null)) {
				tempUser.setSocialnumber(user.getSocialnumber());
			}
			tempUser.setOffice(user.getOffice());
			tempUser.setTelephone(user.getTelephone());
			
			return tempUser;
		}
		return null;
	}
	
	public List<User> listUsers(EntityManager entityManager) throws SystemException {
		
		Query query = entityManager.createNamedQuery(QueryConstants.QUERY_LIST_USERS);
		
		return query.getResultList();
	}
	
	public User getBillsBySeller(User seller, EntityManager entityManager) throws SystemException {
		User foundSeller = this.searchUserByLogin(seller, entityManager);
		
		if (!(foundSeller == null)) {
			Query query = entityManager.createNamedQuery(QueryConstants.QUERY_LIST_BILLS_BY_SELLER);
			query.setParameter(1, foundSeller);
			
			List<Bill> bills = query.getResultList();
			
			SalesBO salesBO = new SalesBO();
			for (Bill bill : bills) {
				List<Sale> sales = salesBO.listSalesByBill(bill, entityManager);
				
				bill.setSaleCollection(sales);
			}
			
			foundSeller.setBillCollection(bills);
			
			return foundSeller;
		}
		return null;	
	}
}
